﻿
CKEDITOR.plugins.add('managedlink',
{
    init: function (editor) {
        editor.addCommand('managedlink', new CKEDITOR.dialogCommand('managedlink'));
        editor.ui.addButton('ManagedLink',
		{
		    label: 'Managed Link',
		    command: 'managedlink',
            style: 'background-color: Red;'
		});

        CKEDITOR.dialog.add('managedlink', this.path + 'dialogs/managedlink.js');

        editor.on('doubleclick', function (evt) {
            var element = CKEDITOR.plugins.link.getSelectedLink(editor) || evt.data.element;

            if (!element.isReadOnly()) {
                if (element.is('a')) {
                    if (element.getAttribute('pageid')) {
                        evt.stop();
                        evt.data.dialog = 'managedlink';
                        editor.getSelection().selectElement(element);
                    }
                }
            }
        });

        // If the "menu" plugin is loaded, register the menu item.
        if (editor.addMenuItems) {
            editor.addMenuItems(
			{
			    managedlink:
				{
				    label: 'Managed Link',
				    command: 'managedlink',
				    group: 'link',
				    order: 3
				}
			});
        }

        // If the "contextmenu" plugin is loaded, register the listeners.
        if (editor.contextMenu) {
            editor.contextMenu.addListener(function (element, selection) {
                if (!element || element.isReadOnly())
                    return null;

                var anchor = CKEDITOR.plugins.link.getSelectedLink(editor, element);

                if (!anchor)
                    return null;

                var menu = {};

                if (anchor.getAttribute('pageid')) {
                    menu = { managedlink: CKEDITOR.TRISTATE_OFF, link: CKEDITOR.TRISTATE_DISABLED };
                }

                return menu;
            });
        }
    },

    requires: ['link']
});